//
//  WWLeftCellModel.m
//  Shopping
//
//  Created by 张德华 on 8/20/16.
//  Copyright © 2016 张湾湾. All rights reserved.
//

#import "WWLeftCellModel.h"
#import "WWNetworkTools.h"
#import "WWRightCellModel.h"

@implementation WWLeftCellModel

+ (instancetype)leftCellModelWithDict:(NSDictionary *)dict {
    WWLeftCellModel *model = [[WWLeftCellModel alloc] init];
    [model setValuesForKeysWithDictionary:dict];
    return model;
}


#pragma mark
#pragma mark - ***************中文输出和没有使用的key
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{}

- (NSString *)description {
    return [NSString stringWithFormat:@"name: %@, ID: %@",self.name,self.id];
}



#pragma mark
#pragma mark - ***************网络请求数据
+ (void)leftModelWithDataSuccess:(void(^)(NSArray<WWLeftCellModel *> *array))successLeft  failure:(void(^)(NSError *error))failure {
    
    
    [[WWNetworkTools sharedTools] surpmarketWithSuccess:^(id response) {
        NSLog(@"%@",response);
        //------ 商品分类数据,左列表
        NSMutableArray *mArray = [NSMutableArray array];
        NSArray *tempArray = response[@"data"][@"categories"];
        for (NSDictionary *dict in tempArray) {
            WWLeftCellModel *model = [WWLeftCellModel leftCellModelWithDict:dict];
            [mArray addObject:model];
        }

        //------ 遍历获取商品详细数据,右边列表
        [mArray enumerateObjectsUsingBlock:^(WWLeftCellModel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            @autoreleasepool {
                NSMutableArray *rightMArray = [NSMutableArray array];
                
                NSArray *tempArray = response[@"data"][@"products"][obj.id];
                
                for (NSDictionary *dict in tempArray) {
                    WWRightCellModel *model = [WWRightCellModel rightCellModelWithDict:dict];
                    [rightMArray addObject:model];
                }
                obj.rightArray = rightMArray.copy;
            }
        }];
        
        //------ 回调数据,刷新列表
        if (successLeft) {
            successLeft(mArray.copy);
        }
        
        
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}

@end
